﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;

namespace RockCompany.Common.Forms
{
    public partial class ParentFrm : Form
    {
        private string sUserName; // login user
        private Form caller;

        public ParentFrm()
        {
            InitializeComponent();
        }

        public ParentFrm(string sUName, Form caller)
        {
            this.sUserName = sUName;
            this.caller = caller;
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.FixedSingle;
        }

        private void picLogout_Click(object sender, EventArgs e)
        {
            CloseAllForm();
        }

        private void ParentFrm_Load(object sender, EventArgs e)
        {
            this.lbLoginUser.Text = "Welcome " + this.sUserName;
        }

        /// <summary>
        /// Close all current open forms.
        /// </summary>
        private void CloseAllForm()
        {
            FormCollection forms = Application.OpenForms;
            while (forms.Count > 1)
            {
                if (!forms[forms.Count - 1].IsDisposed)
                {
                    if (forms[forms.Count - 1].Name.Equals("SC02"))
                    {
                        forms[forms.Count - 1].Show();
                    }
                    else
                    {
                        forms[forms.Count - 1].Close();
                    }
                }
            }
        }

        private void ParentFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (caller != null && !caller.IsDisposed)
            {
                caller.Show();
            } 
            this.Dispose();
        }
    }
}
